<script lang="ts" setup>
import { computed } from "vue";
const props = defineProps<{
  data: {
    value: number;
    interval?: number[];
    status: number;
    jobIdList: any;
    temperature: object;
  };
}>();
const width = computed(() => {
  const { value = 0, interval = [9.3, 10.7] } = props.data;
  let new_value = value;
  if (new_value < interval[0]) {
    new_value = interval[0];
  }
  if (new_value > interval[1]) {
    new_value = interval[1];
  }
  const all = interval[1] - interval[0];
  const bar = new_value - interval[0];
  const val = (bar / all) * 152;
  const width = val < 10 ? 10 : val;
  return width + "px";
});
const temperatureStatu = computed(() => {
  const {
    jobIdList: { data = {} },
    temperature
  } = props.data;
  const jobIdArr = Object.keys(data);
  // temperature为异常温度点位ID，jobIdList任意点位包含在temperature中，则显示异常，否则显示正常
  const flag = Object.keys(temperature).find(item => jobIdArr.includes(item));
  return flag;
});
const flag = computed(() => {
  const { value, interval } = props.data;
  return value < interval[0] || value > interval[1];
});
</script>
<template>
  <div class="propress pl-[16px] pr-[16px]">
    <div class="progress-line-container">
      <div class="progress-line">
        <div :class="['progress-bar', flag ? 'is-error' : ''].join(' ')" :style="`width:${width}`">
          <div class="progress-dot"></div>
        </div>
      </div>
      <div class="progress-legend">
        <span>{{ data.interval[0] }}</span>
        <span>{{ data.interval[1] }}</span>
      </div>
    </div>
    <ul class="list">
      <li class="list-item">
        <div class="list-item-text">状态</div>
        <div class="error" v-if="data.status === 0 || !data.status">关闭</div>
        <div class="normal" v-else>运行</div>
      </li>
      <li class="list-item">
        <div class="list-item-text">温度</div>
        <div class="error" v-if="temperatureStatu">异常</div>
        <div class="normal" v-else>正常</div>
      </li>
    </ul>
  </div>
</template>
<style lang="scss" scoped>
.progress-line-container {
  margin-bottom: 10px;
}
.progress-legend {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  font-weight: 400;
  color: #b1bdca;
  line-height: 12px;
  margin-top: 3px;
}
.progress-line {
  width: 152px;
  height: 6px;
  background: rgba(0, 157, 255, 0.45);
  .progress-bar {
    height: 6px;
    transition: all 0.3 linear;
    background: rgba(0, 157, 255, 1);
    position: relative;
    &.is-error {
      background: #dd5b5b;
      .progress-dot {
        background: #dd5b5b;
      }
    }
  }
  .progress-dot {
    width: 10px;
    height: 10px;
    background: #009dff;
    border: 2px solid #ffffff;
    border-radius: 50%;
    position: absolute;
    top: 50%;
    right: 0;
    transform: translateY(-50%);
  }
}
.list-item-text {
  color: rgba(255, 255, 255, 1);
  font-weight: 400;
  font-size: 12px;
  line-height: 12px;
  margin-right: 8px;
}
.error {
  width: 34px;
  height: 20px;
  background: #dd5b5b;
  border-radius: 4px;
  text-align: center;
  line-height: 20px;
  font-size: 12px;
  color: #fff;
}
.normal {
  width: 34px;
  height: 20px;
  background: #3beb93;
  border-radius: 4px;
  text-align: center;
  line-height: 20px;
  font-size: 12px;
  color: #fff;
}
.list {
  display: flex;
  justify-content: space-between;
}
.list-item {
  display: flex;
  align-items: center;
}
</style>
